package com.yumi.service.Impl;

import com.yumi.service.ZhuhaitongService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yumi.common.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Service
public class ZhuhaitongServiceImpl implements ZhuhaitongService {

    @Override
    public void Download(MultipartFile file, String date, String isEncrypt) {
        try {
            // 获取文件原始名称
            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null) {
                originalFilename = "unknown_file";
            }
            
            // 获取桌面路径
            String desktopPath = FileUtils.getDesktopPath();
            
            // 创建桌面目录
            FileUtils.createDirectoryIfNotExists(desktopPath);
            
            // 如果没有提供日期参数，则使用当前日期
            if (date == null || date.isEmpty()) {
                date = FileUtils.getCurrentDateString();
            }
            
            // 构建新的ZIP文件名：日期_原始文件名.zip
            String zipFilename = FileUtils.buildZipFilename(originalFilename, date);
            String zipFilePath = desktopPath + File.separator + zipFilename;
            
            // 将文件压缩并保存为ZIP
            FileUtils.compressAndSaveFile(file, zipFilePath);
            
            // 记录加密参数日志
            System.out.println("加密参数值: " + (isEncrypt != null ? isEncrypt : "未提供"));
            
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("文件下载失败: " + e.getMessage());
        }
    }
}